package com.pan.admin.generator.service;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.generator.dao.ApiGenWorkBankDao;
import com.pan.admin.generator.entity.ApiGenField;
import com.pan.admin.generator.entity.ApiGenWorkBankPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ApiGenWorkBankServiceImpl implements ApiGenWorkBankService {

    @Autowired
    private ApiGenWorkBankDao apiGenWorkBankDao;

    @Override
    public void insertMappingWorkBank(List<ApiGenField> listGenField) {
        if (ValidParam.isEmpty(listGenField)) {
            return ;
        }

        List<ApiGenWorkBankPO> listApiGenWorkBankPO = CollUtils.newArrayList(listGenField.size());
        IteratorUtils.array(listGenField, genField -> {
            ApiGenWorkBankPO apiGenWorkBankPO = new ApiGenWorkBankPO();
            apiGenWorkBankPO.setFieldName(genField.getFieldName());
            apiGenWorkBankPO.setMappingName(genField.getMappingName());
            apiGenWorkBankPO.setCreateTime(DateUtils.getNowDate());
            listApiGenWorkBankPO.add(apiGenWorkBankPO);
        });
        //  插入
        apiGenWorkBankDao.batchInsert(listApiGenWorkBankPO);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ApiGenWorkBankPO> list(String fieldName) {
        return apiGenWorkBankDao.list(fieldName);
    }
}
